﻿using System;
using System.Html.DOM;

namespace RexToy.UI.Controls
{
    public class TextBox : Control
    {
        public override Palette Palette
        {
            get
            {
                return __palette;
            }
            set
            {
                var dom = this.__domNode.SearchByName("input");
                var template = "ui-body-{0}";
                dom.ReplaceClass(StringExt.Format(template, this.__palette), StringExt.Format(template, value));
                this.__palette = value;
            }
        }

        private bool __enable;
        public bool Enable
        {
            get { return __enable; }
            set
            {
                if (value)
                {
                    this.__domNode.AppendClass("ui-disabled");
                }
                else
                {
                    this.__domNode.AppendClass("ui-disabled");
                }

                this.__enable = value;
                this.Readonly = !value;
            }
        }

        private bool __readonly;
        public bool Readonly
        {
            get
            {
                return __readonly;
            }
            set
            {
                this.__readonly = value;
                var dom = this.__domNode.SearchByName("input");

                if (value)
                    dom.SetAttribute("readOnly", true);
                else
                    dom.SetAttribute("readOnly", null);
            }
        }


        private string __text;
        public string Text
        {
            get
            {
                var input = this.__domNode.SearchByName("input")._element as System.Html.DOM.InputText;
                this.__text = input.Value;
                return this.__text;
            }
            set
            {
                this.__text = value;
                var input = this.__domNode.SearchByName("input")._element as System.Html.DOM.InputText;
                input.Value = value;

                if (this.__enable)
                    this.OnKeyUp();
            }
        }

        public string[] events = new string[] { "onblur", "ontextchange" };

        public TextBox(string text, string hint, string placeholder)
        {
            this.__selectable = true;
            this.__text = text ?? String.Empty;
            this.__palette = Palette.C;
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.Div,
                className = "ui-field-contain ui-body ui-br",
                items = new DomNodeCreateOptions[]{
                new DomNodeCreateOptions()
                {
                    tag= Tag.Label,
                    hashName= "hint",
                    className= "ui-input-text",
                    innerHTML= hint
                },
                new DomNodeCreateOptions()
                {
                    tag= Tag.Input,
                    hashName= "input",
                    className= "ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c",
                    placeholder= placeholder ?? "Input Value",
                    value= this.__text
                }
            }
            });
            var input = this.__domNode.SearchByName("input");
            input.AttachEvent(DomEvent.OnKeyUp, new Action(this.OnKeyUp).Bind(this)).AttachEvent(DomEvent.OnBlur, new Action(this.OnBlur).Bind(this));
        }


        public void SetInputType(InputType type)
        {
            var dom = this.__domNode.SearchByName("input");
            var input = dom._element as System.Html.DOM.InputText;            
            try
            {
                if (type == (InputType)InputType.Date)
                {
                    input.Type = InputType.Text;
                }
                else
                {
                    input.Type = type;
                }
            }
            catch
            {
                //Note: if not a valid type, degrade to text
                input.Type = InputType.Text;
            }

            if (this.__showCalendar == null)
            {
                this.__showCalendar = new Action(this.ShowCalendar).Bind(this);
            }

            if (type == (InputType)InputType.Date)
            {
                dom.AttachEvent(DomEvent.OnFocus, this.__showCalendar);
            }
            else
            {
                dom.DetachEvent(DomEvent.OnFocus, this.__showCalendar);
            }
        }

        private Action __showCalendar;
        private Calendar calendar;
        private void ShowCalendar()
        {
            var host = this;
            if (this.calendar == null)
            {
                this.calendar = new RexToy.UI.Controls.Calendar();
                this.calendar.RenderInto();
                Action<object> func = delegate(object sender)
                {
                    var c = sender as Calendar;
                    var d = new RexToy.L10N.Date { year = c.Year, month = c.Month, date = c.Date };
                    var v = RexToy.L10N.DateFormatter.Format(d);
                    host.Text = v;
                };
                this.calendar.AttachEvent("onselect", func);
            }

            if (!StringExt.IsNullOrEmpty(this.Text))
            {
                var val = RexToy.L10N.DateFormatter.Parse(this.Text);
                this.calendar.Year = val.year;
                this.calendar.Month = val.month;
                this.calendar.Date = val.date;
            }
            var dom = this.__domNode.SearchByName("input");
            var x = dom.GetAbsoluteX();
            var y = dom.GetAbsoluteY() + dom.Height;
            this.calendar.Show(new RexToy.UI.Point(x, y));
        }

        private void OnKeyUp()
        {
            this.__FireEvent("ontextchange");
        }

        private void OnBlur()
        {
            this.__FireEvent("onblur");
        }
    }
}
